#include "sleefsmodel.h"
#include <QBrush>
#include <QDebug>
#include <QDateTime>

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QColor SleeFSModel::mqc_normal(0, 255, 0);
QColor SleeFSModel::mqc_3days(60, 215, 20);
QColor SleeFSModel::mqc_1day(120, 176, 40);
QColor SleeFSModel::mqc_12h(195, 138, 60);
QColor SleeFSModel::mqc_fresh(255, 100, 80);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------

SleeFSModel::SleeFSModel(QObject *parent) :
    QFileSystemModel(parent)
{
    setFilter(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QVariant SleeFSModel::get_text_color(const QModelIndex &index) const
{
    if(!index.isValid()) return QVariant();

    if(mql_selected.contains(filePath(index)))
    {
        return QVariant(QBrush(Qt::yellow));
    }

    QFileInfo qfi = fileInfo(index);
    int min = qfi.lastModified().secsTo(QDateTime::currentDateTime()) / 60;

    if(min < 0) return QVariant(QBrush(Qt::darkGreen));

    if(min > 10080) return QVariant(QBrush(mqc_normal)); //1 week

    if(min > 4320) return QVariant(QBrush(mqc_3days));

    if(min > 1440) return QVariant(QBrush(mqc_1day));

    if(min > 720) return QVariant(QBrush(mqc_12h));

    return QVariant(QBrush(mqc_fresh)); //fresh

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QVariant SleeFSModel::data(const QModelIndex &index, int role) const
{
    switch(role)
    {
    case Qt::ToolTipRole:
        return QVariant(this->fileName(index));
    case Qt::BackgroundRole:
        return QVariant(QBrush(Qt::black));
    case Qt::ForegroundRole:
        return get_text_color(index);
    };


    return QFileSystemModel::data(index, role);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSModel::or_to_select(const QModelIndex &idx)
{
    QString path = filePath(idx);
    if(mql_selected.contains(path))
    {
        mql_selected.removeAt(mql_selected.indexOf(path));
    }
    else
    {
        mql_selected.append(path);
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QStringList SleeFSModel::selected_paths() const
{
    return mql_selected;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

bool SleeFSModel::remove_from_select(const QString &qstr_path)
{
    if(!mql_selected.contains(qstr_path)) return false;

    mql_selected.removeAt(mql_selected.indexOf(qstr_path));
    return true;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------


